package com.flot.yazb.home.setting;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flot.yazb.R;
import com.flot.yazb.base.BActivity;
import com.flot.yazb.base.BApplication;
import com.flot.yazb.model.CityModel;
import com.flot.yazb.model.user.UserModel;
import com.flot.yazb.widget.utils.FileAssertUtils;
import com.flot.yazb.widget.utils.SharedPreferencesUtils;
import com.flot.yazb.widget.WheelDialog;
import com.google.gson.Gson;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class ZhengYouActivity extends BActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tltit)
    RelativeLayout tltit;
    @BindView(R.id.tvage)
    TextView tvage;
    @BindView(R.id.llage)
    LinearLayout llage;
    @BindView(R.id.tvheight)
    TextView tvheight;
    @BindView(R.id.llheight)
    LinearLayout llheight;
    @BindView(R.id.tvedu)
    TextView tvedu;
    @BindView(R.id.lledu)
    LinearLayout lledu;
    @BindView(R.id.tvmoney)
    TextView tvmoney;
    @BindView(R.id.llmoney)
    LinearLayout llmoney;
    @BindView(R.id.tvaddress)
    TextView tvaddress;
    @BindView(R.id.lladdress)
    LinearLayout lladdress;
    @BindView(R.id.ok)
    Button ok;
    private UserModel selfModel = BApplication.getInstance().getUserModel();
    private int parms_age_id = 2;
    private int parms_height_id = 2;
    private int parms_edu_id = 2;
    private int parms_income_id = 2;
    private int parms_address_id = 2;
    private String[] ageArry = new String[]{"不限", "18-25", "26-35", "36-45", "46-55", "55以上"};
    private String[] heightArray = new String[]{"不限", "160以下", "161-165", "166-170", "170以上"};
    private String[] eduArray = new String[]{"不限", "高中,中专及以上", "大专及以上", "本科及以上"};
    private String[] incomeArray = new String[]{"不限", "2000以上", "5000以上", "10000以上"};


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frieng_select);
        ButterKnife.bind(this);
        getMsg();
    }

    /**
     * 获取征友条件信息
     */
    private void getMsg() {
//        showProgress();
        String json = FileAssertUtils.readAssets(this, "province.json");
        Gson gson = new Gson();
        final CityModel cityModel = gson.fromJson(json, CityModel.class);


        tvage.setText( SharedPreferencesUtils.getString(activity,"zy_age",ageArry[0]));
        tvheight.setText(SharedPreferencesUtils.getString(activity,"zy_height",heightArray[0]));
        tvedu.setText(SharedPreferencesUtils.getString(activity,"zy_education",eduArray[0]));
        tvmoney.setText(SharedPreferencesUtils.getString(activity,"zy_income",incomeArray[0]));
        tvaddress.setText(SharedPreferencesUtils.getString(activity,"zy_province","请选择"));

//        SelectFriendModel.getFriendModel(selfModel.id, new BModel.Transit<SelectFriendModel>(this) {
//            @Override
//            public void onBorn(SelectFriendModel serializable, String code, String message) {
//                dismissProgress();
//                if (code.equals(CodeStub.B_OK)) {
//                    if (serializable != null) {
//                        if (serializable.age != null || "".equals(serializable.age)){
//                            parms_age_id=Integer.parseInt(serializable.age);
//                            tvage.setText(ageArry[(Integer.parseInt(serializable.age))]);
//                        }
//
//                        if (serializable.height != null || "".equals(serializable.height)){
//                            parms_height_id=Integer.parseInt(serializable.height);
//                            tvheight.setText(heightArray[(Integer.parseInt(serializable.height))] + "cm");
//                        }
//
//                        if (serializable.education != null || "".equals(serializable.education)){
//                            parms_edu_id=Integer.parseInt(serializable.education);
//                            tvedu.setText(eduArray[(Integer.parseInt(serializable.education))]);
//                        }
//
//                        if (serializable.income != null || "".equals(serializable.income)){
//                            parms_income_id=Integer.parseInt(serializable.income);
//                            tvmoney.setText(incomeArray[(Integer.parseInt(serializable.income))]);
//                        }
//
//                        if (serializable.province != null || "".equals(serializable.province)){
//                            parms_address_id=Integer.parseInt(serializable.province);
//                            for (int i=0;i<cityModel.getIdList().size();i++){
//                                if (cityModel.getIdList().get(i).equals(serializable.province)){
//                                    tvaddress.setText(cityModel.getStringList().get(i));
//                                }
//                            }
//                        }
//
//                    }
//                } else {
//                    ToastUtil.showToast(activity, message);
//                }
//            }
//        });
    }

    @OnClick({R.id.iv_back, R.id.llage, R.id.llheight, R.id.lledu, R.id.llmoney, R.id.lladdress, R.id.ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.llage:
                WheelDialog dia = new WheelDialog(this, Arrays.asList(ageArry), parms_age_id);
                dia.setOnSelectedListener(new WheelDialog.selectedEnd() {
                    @Override
                    public void getSelected(String text) {
                        tvage.setText(text);
                        SharedPreferencesUtils.saveString(activity,"zy_age",text);
                    }

                    @Override
                    public void getPosition(int position) {
                        parms_age_id = position;
                    }
                });
                dia.show();
                break;
            case R.id.llheight:
                WheelDialog dia1 = new WheelDialog(this, Arrays.asList(heightArray), parms_height_id);
                dia1.setOnSelectedListener(new WheelDialog.selectedEnd() {
                    @Override
                    public void getSelected(String text) {
                        tvheight.setText(text + "cm");
                        SharedPreferencesUtils.saveString(activity,"zy_height",text);
                    }

                    @Override
                    public void getPosition(int position) {
                        parms_height_id = position;
                    }
                });
                dia1.show();
                break;
            case R.id.lledu:
                WheelDialog dia3 = new WheelDialog(this, Arrays.asList(eduArray), parms_edu_id);
                dia3.setOnSelectedListener(new WheelDialog.selectedEnd() {
                    @Override
                    public void getSelected(String text) {
                        tvedu.setText(text);
                        SharedPreferencesUtils.saveString(activity,"zy_education",text);
                    }

                    @Override
                    public void getPosition(int position) {
                        parms_edu_id = position;
                    }
                });
                dia3.show();
                break;
            case R.id.llmoney:
                WheelDialog dia4 = new WheelDialog(this, Arrays.asList(incomeArray), parms_income_id);
                dia4.setOnSelectedListener(new WheelDialog.selectedEnd() {
                    @Override
                    public void getSelected(String text) {
                        tvmoney.setText(text);
                        SharedPreferencesUtils.saveString(activity,"zy_income",text);
                    }

                    @Override
                    public void getPosition(int position) {
                        parms_income_id = position;
                    }
                });
                dia4.show();
                break;
            case R.id.lladdress:
                String json = FileAssertUtils.readAssets(this, "province.json");
                Gson gson = new Gson();
                final CityModel cityModel = gson.fromJson(json, CityModel.class);
                WheelDialog dia5 = new WheelDialog(this, cityModel.getStringList(), parms_address_id);
                dia5.setOnSelectedListener(new WheelDialog.selectedEnd() {
                    @Override
                    public void getSelected(String text) {
                        tvaddress.setText(text);
                        SharedPreferencesUtils.saveString(activity,"zy_province",text);
                    }

                    @Override
                    public void getPosition(int position) {
                        parms_address_id = Integer.parseInt(cityModel.getIdList().get(position));
                    }
                });
                dia5.show();
                break;
            case R.id.ok:
                showProgress();
                Map map=new HashMap();
                map.put("mid",selfModel.id);
                map.put("age",parms_age_id);
                map.put("height",parms_height_id);
                map.put("education",parms_edu_id);
                map.put("income",parms_income_id);
                map.put("province",parms_address_id);

                dismissProgress();
                finish();
//                SelectFriendModel.setFriendModel(map, new BModel.Transit<SelectFriendModel>(this) {
//                    @Override
//                    public void onBorn(SelectFriendModel serializable, String code, String message) {
//                        dismissProgress();
//                        if (code.equals(CodeStub.B_OK)){
//                         finish();
//                        }else{
//                            ToastUtil.showToast(activity,message);
//                        }
//                    }
//                });
                break;
        }
    }
}
